//
//  CZNxetWorkTool.swift
//  XLWB009
//
//  Created by apple on 3/23/16.
//  Copyright © 2016 apple. All rights reserved.
//

import UIKit
import AFNetworking

class CZNxetWorkTool: NSObject {

   //单例
    static let sharedInstance:CZNxetWorkTool=CZNxetWorkTool()
    
    //afn
    let afnManager:AFHTTPSessionManager=AFHTTPSessionManager(baseURL:CZBaseURL)
    
    //私有化构造函数
    private override init(){
        super.init()
        //设置afn支持text/plain
    afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }
    
    //自己封装GET请求
    func requestGET(urlString:String,parameters:AnyObject?,requestCallback:(result:[String:AnyObject]?,error:NSError?)->()){
        
        afnManager.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
        
            //网络请求回来的都是JSON格式
        let dict=result as? [String:AnyObject]
        requestCallback(result: dict, error: nil)
            
        }) { (_, error) -> Void in
        
            requestCallback(result: nil, error: nil)
            
        }
    }
    
    //自己封装POST请求
    func requestPOST(urlString:String,parameters:AnyObject?,requestCallback:(result:[String:AnyObject]?,error:NSError?)->()){
        
        afnManager.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            //网络请求回来都是JSON格式
            let dict=result as? [String:AnyObject]
            requestCallback(result: dict, error: nil)
            
            }) { (_, error) -> Void in
                
            requestCallback(result: nil, error: nil)
        }
    }
}
